Introducing Functions That Return Data

OPENING QUESTION: We explored simple functions such as msg(). Then we worked with functions that receive an argument such as calcArea(rad), where a value for the radius is 'passed into' the function and used there. Then we expanded that to include to different arguments passed in: calcVals(rad, type) where rad is the radius and type is a variable that contains the user's choice of whether to calculate the circumference of a circle (type =c) or the area of the circle (type = a).

How many arguments can a function take?

If a function takes multiple arguments, the name of the argument is critical. Why is that statement false?

OBJECTIVES: I will work with my team to analyze and then write a function that accepts arguments and returns a value during today's class.

CALENDAR:

WORDS O' THE DAY:

  • Argument (A value 'sent in' to a function to be used there)
  • Parameters (A 'placeholder variable inside a function that 'receives' the argument 'passed into' the function)

WORK O' THE DAY

The last feature of functions we need to learn is the 'return' command.

Functions are often used to process specific data. It is often very helpful to have the results of the work of that function RETURNED back to the main program.

The syntax for that is kind of odd:

var result = calculateArea(radius)

A written description of that process would look like this:

  • define a variable name result.
  • run the function calculateArea and send in the value of the variable radius into that function
  • take the result of that function and set it equal to the variable named result

Here is some code to ponder. Please comment as appropriate. There is a bug there. Find it. Fix it.

This type of function will come in VERY handy in pong when you need to share values between your main program and other functions